<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="utf-8">
<!-- calqlatr.qdoc -->
  <title>Qt Quick Demo - Calqlatr | Qt 5.14</title>
  <link rel="stylesheet" type="text/css" href="style/offline-simple.css" />
  <script type="text/javascript">
    document.getElementsByTagName("link").item(0).setAttribute("href", "style/offline.css");
    // loading style sheet breaks anchors that were jumped to before
    // so force jumping to anchor again
    setTimeout(function() {
        var anchor = location.hash;
        // need to jump to different anchor first (e.g. none)
        location.hash = "#";
        setTimeout(function() {
            location.hash = anchor;
        }, 0);
    }, 0);
  </script>
</head>
<body>
<div class="header" id="qtdocheader">
  <div class="main">
    <div class="main-rounded">
      <div class="navigationbar">
        <table><tr>
<td ><a href="index.html">Qt 5.14</a></td><td >Qt Quick Demo - Calqlatr</td></tr></table><table class="buildversion"><tr>
<td id="buildversion" width="100%" align="right">Qt 5.14.2 Reference Documentation</td>
        </tr></table>
      </div>
    </div>
<div class="content">
<div class="line">
<div class="content mainContent">
<h1 class="title">Qt Quick Demo - Calqlatr</h1>
<span class="subtitle"></span>
<pre class="qml">

  <span class="comment">/****************************************************************************
  **
  ** Copyright (C) 2017 The Qt Company Ltd.
  ** Contact: https://www.qt.io/licensing/
  **
  ** This file is part of the examples of the Qt Toolkit.
  **
  ** $QT_BEGIN_LICENSE:BSD$
  ** Commercial License Usage
  ** Licensees holding valid commercial Qt licenses may use this file in
  ** accordance with the commercial license agreement provided with the
  ** Software or, alternatively, in accordance with the terms contained in
  ** a written agreement between you and The Qt Company. For licensing terms
  ** and conditions see https://www.qt.io/terms-conditions. For further
  ** information use the contact form at https://www.qt.io/contact-us.
  **
  ** BSD License Usage
  ** Alternatively, you may use this file under the terms of the BSD license
  ** as follows:
  **
  ** &quot;Redistribution and use in source and binary forms, with or without
  ** modification, are permitted provided that the following conditions are
  ** met:
  **   * Redistributions of source code must retain the above copyright
  **     notice, this list of conditions and the following disclaimer.
  **   * Redistributions in binary form must reproduce the above copyright
  **     notice, this list of conditions and the following disclaimer in
  **     the documentation and/or other materials provided with the
  **     distribution.
  **   * Neither the name of The Qt Company Ltd nor the names of its
  **     contributors may be used to endorse or promote products derived
  **     from this software without specific prior written permission.
  **
  **
  ** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  ** &quot;AS IS&quot; AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  ** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  ** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  ** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  ** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  ** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  ** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  ** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  ** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  ** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.&quot;
  **
  ** $QT_END_LICENSE$
  **
  ****************************************************************************/</span>

  import QtQuick 2.0
  import &quot;content&quot;
  import &quot;content/calculator.js&quot; as CalcEngine

  <span class="type"><a href="../qtquick/qml-qtquick-rectangle.html">Rectangle</a></span> {
      <span class="name">id</span>: <span class="name">window</span>
      <span class="name">width</span>: <span class="number">320</span>
      <span class="name">height</span>: <span class="number">480</span>
      <span class="name">focus</span>: <span class="number">true</span>
      <span class="name">color</span>: <span class="string">&quot;#272822&quot;</span>

      <span class="name">onWidthChanged</span>: <span class="name">controller</span>.<span class="name">reload</span>()
      <span class="name">onHeightChanged</span>: <span class="name">controller</span>.<span class="name">reload</span>()

      <span class="keyword">function</span> <span class="name">operatorPressed</span>(operator) {
          <span class="name">CalcEngine</span>.<span class="name">operatorPressed</span>(<span class="name">operator</span>)
          <span class="name">numPad</span>.<span class="name">buttonPressed</span>()
      }
      <span class="keyword">function</span> <span class="name">digitPressed</span>(digit) {
          <span class="name">CalcEngine</span>.<span class="name">digitPressed</span>(<span class="name">digit</span>)
          <span class="name">numPad</span>.<span class="name">buttonPressed</span>()
      }
      <span class="keyword">function</span> <span class="name">isButtonDisabled</span>(op) {
          <span class="keyword">return</span> <span class="name">CalcEngine</span>.<span class="name">disabled</span>(<span class="name">op</span>)
      }

      <span class="type"><a href="../qtquick/qml-qtquick-item.html">Item</a></span> {
          <span class="name">id</span>: <span class="name">pad</span>
          <span class="name">width</span>: <span class="number">180</span>
          <span class="type">NumberPad</span> { <span class="name">id</span>: <span class="name">numPad</span>; <span class="name">y</span>: <span class="number">10</span>; <span class="name">anchors</span>.horizontalCenter: <span class="name">parent</span>.<span class="name">horizontalCenter</span> }
      }

      <span class="type"><a href="../qt3d/qml-qt3d-animation-animationcontroller.html">AnimationController</a></span> {
          <span class="name">id</span>: <span class="name">controller</span>
          <span class="name">animation</span>: <span class="name">ParallelAnimation</span> {
              <span class="name">id</span>: <span class="name">anim</span>
              <span class="type"><a href="../qtquick/qml-qtquick-numberanimation.html">NumberAnimation</a></span> { <span class="name">target</span>: <span class="name">display</span>; <span class="name">property</span>: <span class="string">&quot;x&quot;</span>; <span class="name">duration</span>: <span class="number">400</span>; <span class="name">from</span>: -<span class="number">16</span>; <span class="name">to</span>: <span class="name">window</span>.<span class="name">width</span> <span class="operator">-</span> <span class="name">display</span>.<span class="name">width</span>; <span class="name">easing</span>.type: <span class="name">Easing</span>.<span class="name">InOutQuad</span> }
              <span class="type"><a href="../qtquick/qml-qtquick-numberanimation.html">NumberAnimation</a></span> { <span class="name">target</span>: <span class="name">pad</span>; <span class="name">property</span>: <span class="string">&quot;x&quot;</span>; <span class="name">duration</span>: <span class="number">400</span>; <span class="name">from</span>: <span class="name">window</span>.<span class="name">width</span> <span class="operator">-</span> <span class="name">pad</span>.<span class="name">width</span>; <span class="name">to</span>: <span class="number">0</span>; <span class="name">easing</span>.type: <span class="name">Easing</span>.<span class="name">InOutQuad</span> }
              <span class="type"><a href="../qtquick/qml-qtquick-sequentialanimation.html">SequentialAnimation</a></span> {
                  <span class="type"><a href="../qtquick/qml-qtquick-numberanimation.html">NumberAnimation</a></span> { <span class="name">target</span>: <span class="name">pad</span>; <span class="name">property</span>: <span class="string">&quot;scale&quot;</span>; <span class="name">duration</span>: <span class="number">200</span>; <span class="name">from</span>: <span class="number">1</span>; <span class="name">to</span>: <span class="number">0.97</span>; <span class="name">easing</span>.type: <span class="name">Easing</span>.<span class="name">InOutQuad</span> }
                  <span class="type"><a href="../qtquick/qml-qtquick-numberanimation.html">NumberAnimation</a></span> { <span class="name">target</span>: <span class="name">pad</span>; <span class="name">property</span>: <span class="string">&quot;scale&quot;</span>; <span class="name">duration</span>: <span class="number">200</span>; <span class="name">from</span>: <span class="number">0.97</span>; <span class="name">to</span>: <span class="number">1</span>; <span class="name">easing</span>.type: <span class="name">Easing</span>.<span class="name">InOutQuad</span> }
              }
          }
      }

      <span class="name">Keys</span>.onPressed: {
          <span class="keyword">if</span> (<span class="name">event</span>.<span class="name">key</span> <span class="operator">==</span> <span class="name">Qt</span>.<span class="name">Key_0</span>)
              <span class="name">digitPressed</span>(<span class="string">&quot;0&quot;</span>)
          <span class="keyword">else</span> <span class="keyword">if</span> (<span class="name">event</span>.<span class="name">key</span> <span class="operator">==</span> <span class="name">Qt</span>.<span class="name">Key_1</span>)
              <span class="name">digitPressed</span>(<span class="string">&quot;1&quot;</span>)
          <span class="keyword">else</span> <span class="keyword">if</span> (<span class="name">event</span>.<span class="name">key</span> <span class="operator">==</span> <span class="name">Qt</span>.<span class="name">Key_2</span>)
              <span class="name">digitPressed</span>(<span class="string">&quot;2&quot;</span>)
          <span class="keyword">else</span> <span class="keyword">if</span> (<span class="name">event</span>.<span class="name">key</span> <span class="operator">==</span> <span class="name">Qt</span>.<span class="name">Key_3</span>)
              <span class="name">digitPressed</span>(<span class="string">&quot;3&quot;</span>)
          <span class="keyword">else</span> <span class="keyword">if</span> (<span class="name">event</span>.<span class="name">key</span> <span class="operator">==</span> <span class="name">Qt</span>.<span class="name">Key_4</span>)
              <span class="name">digitPressed</span>(<span class="string">&quot;4&quot;</span>)
          <span class="keyword">else</span> <span class="keyword">if</span> (<span class="name">event</span>.<span class="name">key</span> <span class="operator">==</span> <span class="name">Qt</span>.<span class="name">Key_5</span>)
              <span class="name">digitPressed</span>(<span class="string">&quot;5&quot;</span>)
          <span class="keyword">else</span> <span class="keyword">if</span> (<span class="name">event</span>.<span class="name">key</span> <span class="operator">==</span> <span class="name">Qt</span>.<span class="name">Key_6</span>)
              <span class="name">digitPressed</span>(<span class="string">&quot;6&quot;</span>)
          <span class="keyword">else</span> <span class="keyword">if</span> (<span class="name">event</span>.<span class="name">key</span> <span class="operator">==</span> <span class="name">Qt</span>.<span class="name">Key_7</span>)
              <span class="name">digitPressed</span>(<span class="string">&quot;7&quot;</span>)
          <span class="keyword">else</span> <span class="keyword">if</span> (<span class="name">event</span>.<span class="name">key</span> <span class="operator">==</span> <span class="name">Qt</span>.<span class="name">Key_8</span>)
              <span class="name">digitPressed</span>(<span class="string">&quot;8&quot;</span>)
          <span class="keyword">else</span> <span class="keyword">if</span> (<span class="name">event</span>.<span class="name">key</span> <span class="operator">==</span> <span class="name">Qt</span>.<span class="name">Key_9</span>)
              <span class="name">digitPressed</span>(<span class="string">&quot;9&quot;</span>)
          <span class="keyword">else</span> <span class="keyword">if</span> (<span class="name">event</span>.<span class="name">key</span> <span class="operator">==</span> <span class="name">Qt</span>.<span class="name">Key_Plus</span>)
              <span class="name">operatorPressed</span>(<span class="string">&quot;+&quot;</span>)
          <span class="keyword">else</span> <span class="keyword">if</span> (<span class="name">event</span>.<span class="name">key</span> <span class="operator">==</span> <span class="name">Qt</span>.<span class="name">Key_Minus</span>)
              <span class="name">operatorPressed</span>(<span class="string">&quot;−&quot;</span>)
          <span class="keyword">else</span> <span class="keyword">if</span> (<span class="name">event</span>.<span class="name">key</span> <span class="operator">==</span> <span class="name">Qt</span>.<span class="name">Key_Asterisk</span>)
              <span class="name">operatorPressed</span>(<span class="string">&quot;×&quot;</span>)
          <span class="keyword">else</span> <span class="keyword">if</span> (<span class="name">event</span>.<span class="name">key</span> <span class="operator">==</span> <span class="name">Qt</span>.<span class="name">Key_Slash</span>)
              <span class="name">operatorPressed</span>(<span class="string">&quot;÷&quot;</span>)
          <span class="keyword">else</span> <span class="keyword">if</span> (<span class="name">event</span>.<span class="name">key</span> <span class="operator">==</span> <span class="name">Qt</span>.<span class="name">Key_Enter</span> <span class="operator">||</span> <span class="name">event</span>.<span class="name">key</span> <span class="operator">==</span> <span class="name">Qt</span>.<span class="name">Key_Return</span>)
              <span class="name">operatorPressed</span>(<span class="string">&quot;=&quot;</span>)
          <span class="keyword">else</span> <span class="keyword">if</span> (<span class="name">event</span>.<span class="name">key</span> <span class="operator">==</span> <span class="name">Qt</span>.<span class="name">Key_Comma</span> <span class="operator">||</span> <span class="name">event</span>.<span class="name">key</span> <span class="operator">==</span> <span class="name">Qt</span>.<span class="name">Key_Period</span>)
              <span class="name">digitPressed</span>(<span class="string">&quot;.&quot;</span>)
          <span class="keyword">else</span> <span class="keyword">if</span> (<span class="name">event</span>.<span class="name">key</span> <span class="operator">==</span> <span class="name">Qt</span>.<span class="name">Key_Backspace</span>)
              <span class="name">operatorPressed</span>(<span class="string">&quot;backspace&quot;</span>)
      }

      <span class="type">Display</span> {
          <span class="name">id</span>: <span class="name">display</span>
          <span class="name">x</span>: -<span class="number">16</span>
          <span class="name">width</span>: <span class="name">window</span>.<span class="name">width</span> <span class="operator">-</span> <span class="name">pad</span>.<span class="name">width</span>
          <span class="name">height</span>: <span class="name">parent</span>.<span class="name">height</span>

          <span class="type"><a href="../qtquick/qml-qtquick-mousearea.html">MouseArea</a></span> {
              <span class="name">id</span>: <span class="name">mouseInput</span>
              property <span class="type"><a href="../qtqml/qml-real.html">real</a></span> <span class="name">startX</span>: <span class="number">0</span>
              property <span class="type"><a href="../qtqml/qml-real.html">real</a></span> <span class="name">oldP</span>: <span class="number">0</span>
              property <span class="type"><a href="../qtqml/qml-bool.html">bool</a></span> <span class="name">rewind</span>: <span class="number">false</span>

              <span class="type">anchors</span> {
                  <span class="name">bottom</span>: <span class="name">parent</span>.<span class="name">bottom</span>
                  <span class="name">left</span>: <span class="name">parent</span>.<span class="name">left</span>
                  <span class="name">right</span>: <span class="name">parent</span>.<span class="name">right</span>
              }
              <span class="name">height</span>: <span class="number">50</span>
              <span class="name">onPositionChanged</span>: {
                  var reverse = <span class="name">startX</span> <span class="operator">&gt;</span> <span class="name">window</span>.<span class="name">width</span> <span class="operator">/</span> <span class="number">2</span>
                  var mx = <span class="name">mapToItem</span>(<span class="name">window</span>, <span class="name">mouseInput</span>.<span class="name">mouseX</span>, <span class="name">mouseInput</span>.<span class="name">mouseY</span>).<span class="name">x</span>
                  var p = <span class="name">Math</span>.<span class="name">abs</span>((<span class="name">mx</span> <span class="operator">-</span> <span class="name">startX</span>) <span class="operator">/</span> (<span class="name">window</span>.<span class="name">width</span> <span class="operator">-</span> <span class="name">display</span>.<span class="name">width</span>))
                  <span class="keyword">if</span> (<span class="name">p</span> <span class="operator">&lt;</span> <span class="name">oldP</span>)
                      <span class="name">rewind</span> <span class="operator">=</span> <span class="name">reverse</span> ? <span class="number">false</span> : <span class="number">true</span>
                  <span class="keyword">else</span>
                      <span class="name">rewind</span> <span class="operator">=</span> <span class="name">reverse</span> ? <span class="number">true</span> : <span class="number">false</span>
                  <span class="name">controller</span>.<span class="name">progress</span> <span class="operator">=</span> <span class="name">reverse</span> ? <span class="number">1</span> <span class="operator">-</span> <span class="name">p</span> : <span class="name">p</span>
                  <span class="name">oldP</span> <span class="operator">=</span> <span class="name">p</span>
              }
              <span class="name">onPressed</span>: <span class="name">startX</span> <span class="operator">=</span> <span class="name">mapToItem</span>(<span class="name">window</span>, <span class="name">mouseInput</span>.<span class="name">mouseX</span>, <span class="name">mouseInput</span>.<span class="name">mouseY</span>).<span class="name">x</span>
              <span class="name">onReleased</span>: {
                  <span class="keyword">if</span> (<span class="name">rewind</span>)
                      <span class="name">controller</span>.<span class="name">completeToBeginning</span>()
                  <span class="keyword">else</span>
                      <span class="name">controller</span>.<span class="name">completeToEnd</span>()
              }
          }
      }

  }

</pre>
